package com.example.spinning.controller;

import com.example.spinning.bean.PaginationResult;
import com.example.spinning.bean.RespBean;
import com.example.spinning.bean.SpinningQualityReport;
import com.example.spinning.service.SpinningQualityReportService;
import com.example.spinning.utils.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/spinningReport")
public class SpinningQualityReportController {
    @Autowired
    SpinningQualityReportService spinningQualityReportService;

    //增加记录
    @Log(module = "细纱质量报表",description = "增加细纱质量报表")
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewSpinningQualityReport(SpinningQualityReport spinningQualityReport) {
        if ("".equals(spinningQualityReport.getName())|| spinningQualityReport.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(spinningQualityReport.getBatch_no())|| spinningQualityReport.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = spinningQualityReportService.addNewSpinningQualityReport(spinningQualityReport);
        if (result == 1) {
            return new RespBean("success",  "细纱质量信息添加成功");
        } else {
            return new RespBean("error","添加细纱质量信息成功");
        }
    }

    //根据id删除记录
    @Log(module = "细纱质量报表",description = "删除细纱质量报表")
    @RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
    public RespBean deleteById(Long[] ids) {

        boolean result = spinningQualityReportService.deleteSpinningQualityReportById(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    //查找
    @RequestMapping(value = "/getByMix/", method = RequestMethod.GET)
    public Map<String, Object> findPage(Integer page, Integer size, String batch_no, String name, String client, Date date,String color){
        PaginationResult pr = spinningQualityReportService.findPage(page, size,batch_no,name,client,date,color);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",pr.getTotal());
        map.put("list",pr.getList());
        return map;
    }

    @Log(module = "细纱质量报表",description = "修改细纱质量报表")
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public RespBean updateById(SpinningQualityReport spinningQualityReport){

        int result = spinningQualityReportService.updateCategoryById(spinningQualityReport);
        if (result == 1) {
            return new RespBean("success",  "质量报表修改成功");
        } else {
            return new RespBean("error","质量报表修改失败");
        }
    }



}



